package com.mooc;

import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

public class ArthasMainClass {
    public static Class randomClass = null;

    public static void agentmain(String args, Instrumentation inst) throws UnmodifiableClassException {
        // 保存原始类
        randomClass = new RandomUtil().getClass();
        System.out.println("修改前数据" + RandomUtil.create());

        // 添加Transformer
        RandomTransformer randomTransformer = new RandomTransformer();
        inst.addTransformer(randomTransformer, true);

        // 加载新类
        inst.retransformClasses(RandomUtil.class);
        System.out.println("修改后数据" + RandomUtil.create());

        // 回滚
        inst.removeTransformer(randomTransformer);
        inst.retransformClasses(randomClass);
        System.out.println("回滚后数据" + RandomUtil.create());
    }
}
